package com.emep.changzhi.analyse.http.systeminfo;

import com.emep.changzhi.analyse.entity.systeminfo.SysRoleInfo;
import com.emep.changzhi.analyse.model.Result;
import com.emep.changzhi.analyse.model.ResultBean;
import com.emep.changzhi.analyse.service.systeminfo.SysRoleInfoService;
import com.emep.changzhi.analyse.utils.Constant;
import com.emep.changzhi.analyse.utils.DataValidator;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.Map;

/**
 * @author Wangcc
 * @version V1.0
 * @Description: 角色信息控制器
 * @date 2018/2/24 15:05
 */
@RestController
@RequestMapping("role")
@Api(value="角色controller",tags={"角色信息接口"})
public class SysRoleInfoController {

    @Autowired
    private SysRoleInfoService sysRoleInfoService;

    /**
     * 查询所有角色
     * @return
     */
    @ApiOperation(value="查询所有角色", notes="查询所有角色")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @GetMapping(value = "list")
    public ResultBean<Collection<SysRoleInfo>> queryUser(){
        return new ResultBean<Collection<SysRoleInfo>>(sysRoleInfoService.list());
    }

    /**
     * 新增角色
     * @param payload
     * @return
     */
    @ApiOperation(value="新增角色", notes="新增角色")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "修改时传的id", required = false, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "roleName", value = "角色名称", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "description", value = "描述", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "menuId", value = "菜单id集合 id与id  , 号分割",  required = false, dataType = "String",paramType="query")
    })
    @PostMapping(value = "add")
    public ResultBean<SysRoleInfo> add(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "roleName","description");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
           return result.boolAdd(sysRoleInfoService.add(payload))  ;
        }
        return  result;
    }

    /**
     * 通过id 查询角色
     * @param payload
     * @return
     */
    @ApiOperation(value="通过id查询角色信息", notes="通过id查询角色信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
                    @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
                    @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色id", required = true, dataType = "String",paramType="query"),
    })
    @GetMapping(value = "info")
    public ResultBean<SysRoleInfo> queryUser( @RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "id");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            return result.ok(sysRoleInfoService.findId(payload.get("id").toString())) ;
        }
        return result;
    }

    /**
     * 根据id 删除用户
     * @param payload
     */
    @ApiOperation(value="通过id删除角色信息", notes="通过id删除角色信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色id", required = true, dataType = "String",paramType="query"),
    })
    @DeleteMapping(value = "delete")
    public ResultBean<Boolean> delete(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "id");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
           return result.bool(sysRoleInfoService.delete(payload.get("id").toString())) ;

        }
        return result;
    }

    /**
     * 分页查询用户列表
     * page，第几页，从0开始，默认为第0页
     * size，每一页的大小，默认为20
     * sort，排序相关的信息，以property,property(,ASC|DESC)的方式组织，例如sort=firstname&sort=lastname,desc表示在按firstname正序排列基础上按lastname倒序排列
     * @param
     * @return
     */
    @ApiOperation(value="分页查询角色信息", notes="分页查询角色信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "页数", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "pageSize", value = "数量", required = true, dataType = "String",paramType="query"),
    })
    @GetMapping("paging")
    public ResultBean<Page<SysRoleInfo>> showUsers(@RequestParam Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "pageIndex","pageSize");
        if(result.getCode() == Constant.RESULT_CODE_SUCCESS){
            Page<SysRoleInfo> pageSysUserInfo = sysRoleInfoService.findAll(Integer.parseInt(payload.get("pageIndex")
                    .toString()),Integer.parseInt(payload.get("pageSize").toString()));
            return result.ok(pageSysUserInfo);
        }
        return result;
    }

}
